import pymysql


def savePhotoToBd(photo_name, folder, photo_type, item_name, page_from,category='doors'):

	conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='door_market')

	cur = conn.cursor()

	"""
	item_name = 'test'
	photo_name = 'test.jpj'
	photo_type = 'main'

	folder = 'test_fol'"""


	item_id = cur.execute("select item_id from item where item_name = '" + item_name + "';")
	#print(item_id)
	if item_id == 0 :
		data  =  cur.execute("insert into item(item_name, item_type_id,page_from,category) values ('" + item_name + "','1','"+page_from + "','"+category + "');")
		print(" + item " + item_name + " was inserted into table")
		conn.commit()
	cur.execute("select item_id from item where item_name = '" + item_name + "';")

	item_id_res = cur.fetchone()

	item_id = str(item_id_res).replace('(','').replace(',','').replace(')','')
	print(' item_id = ' +item_id )
    
	photo_id = cur.execute("select id_photo from photo WHERE photo_name='"+ photo_name + "' AND folder ='" + folder + "' LIMIT 0,1;")
	photo_id_res = cur.fetchone()
	photo_id = str(photo_id_res).replace('(','').replace(',','').replace(')','')
	#print("photo_id = "+ str(photo_id))
	if photo_id == 0 or  photo_id == "None":
		data  =  cur.execute("insert into photo(photo_name, photo_type,item_id,folder) values ('" + photo_name + "','"+photo_type + "','" + str(item_id) + "','"+ folder + "');")
		conn.commit()
		#print(" + photo " + photo_name + " was inserted into table")
	#else:
		#print(" - photo " + photo_name + " was NOT inserted into table")
	photo_id = cur.execute("select id_photo from photo WHERE photo_name='"+ photo_name + "' AND folder ='" + folder + "' LIMIT 0,1;")
	photo_id_res = cur.fetchone()
	photo_id = str(photo_id_res).replace('(','').replace(',','').replace(')','')


	cur.close()
	conn.close()
	return photo_id


def getPthotoID(photo_name,folder):
	print("getPthotoID")

	conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='door_market')

	cur = conn.cursor()

	photo_id = cur.execute("select id_photo from photo WHERE photo_name='"+ photo_name + "' AND folder ='" + folder + "' LIMIT 0,1;")
	photo_id_res = cur.fetchone()
	photo_id = str(photo_id_res).replace('(','').replace(',','').replace(')','')

	cur.close()
	conn.close()
	return photo_id

def setCatalogLargePhoto(catalog_photo_id,large_img_id):
	conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='door_market')

	cur = conn.cursor()
	#print("--->  setCatalogLargePhoto: start" )

	res = cur.execute("UPDATE photo SET large_photo_id = " + large_img_id + " where id_photo = "+catalog_photo_id+ " AND photo_type ='catalog_image';commit;")

	#print("--->  setCatalogLargePhoto: " + str(res))

	cur.close()
	conn.close()

def setLargePhoto(photo_id,large_img_id):
	conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='door_market')

	cur = conn.cursor()

	res = cur.execute("UPDATE photo SET large_photo_id = " + large_img_id + " where id_photo = "+photo_id+ ";commit;")

	cur.close()
	conn.close()

def setMiddlePhoto(photo_id,middle_img_id):
	conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='door_market')

	cur = conn.cursor()

	res = cur.execute("UPDATE photo SET middle_photo_id = " + middle_img_id + " where id_photo = "+photo_id+ ";commit;")

	cur.close()
	conn.close()

def setSmallPhoto(photo_id,small_img_id):
	conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='door_market')

	cur = conn.cursor()

	res = cur.execute("UPDATE photo SET small_photo_id = " + small_img_id + " where id_photo = "+photo_id+ ";commit;")

	cur.close()
	conn.close()



